package day09;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class StreamReader {

    public static void main(String[] args) {
//        getFileReader1();
        getFileReader2();
    }

    protected static void getFileReader1() {
        File file = new File("src/test11/hello.txt");
        FileReader reader = null;
        try {
            reader = new FileReader(file);
            int read;
            while ((read = reader.read()) != -1) {
                System.out.print((char) read);
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException exception) {
                    exception.printStackTrace();
                }
            }
        }
    }

    protected static void getFileReader2() {
        File file = new File("src/test11/hello.txt");
        FileReader reader = null;
        try {
            reader = new FileReader(file);
            char[] chars = new char[100]; // 读入字符内
            while ((reader.read(chars)) != -1) {
                System.out.println(chars);
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException exception) {
                    exception.printStackTrace();
                }
            }
        }
    }

}
